package com.hrsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrsystem.entity.DistributedList;
import com.hrsystem.entity.DistributedListDetail;
import com.hrsystem.entity.Employee;
import com.hrsystem.entity.SalaryList;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.DistributedListDetailMapper;
import com.hrsystem.mapper.DistributedListMapper;
import com.hrsystem.service.DistributedListDetailService;
import com.hrsystem.service.DistributedListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("distributed")
public class SalaryDistributed {

    @Autowired
    DistributedListService distributedListService;

    @Autowired
    DistributedListDetailService distributedListDetailService;

    @Resource
    DistributedListMapper distributedListMapper;

    @Resource
    DistributedListDetailMapper distributedListDetailMapper;

    @Transactional
    @GetMapping("/queryFile/{slistId}/{registrant}/{timeValueStart}/{timeValueEnd}")
    public Result queryFile(@PathVariable(name = "slistId") String slistId, @PathVariable(name = "registrant") String registrant, @PathVariable(name = "timeValueStart") String timeValueStart, @PathVariable(name = "timeValueEnd") String  timeValueEnd) throws ParseException {
        QueryWrapper<DistributedList> queryWrapper =new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        if(!slistId.equals("empty")){
            queryWrapper.eq("s_list_id",Long.parseLong(slistId));

        }
        if(!registrant.equals("empty")){
            queryWrapper.eq("Registrant", registrant);
        }
        if (!timeValueStart.equals("undefined") && !timeValueEnd.equals("undefined")){
            Date timeValueStartDate = simpleDateFormat.parse(timeValueStart);
            Date timeValueEndDate = simpleDateFormat.parse(timeValueEnd);
            queryWrapper.between("RegisterTime", timeValueStartDate ,timeValueEndDate);
        }
        queryWrapper.select("distributed_id","s_list_id","fOrg_id","sOrg_id","tOrg_id","TotalSalary");
        List<DistributedList> distributedLists=distributedListMapper.selectList(queryWrapper);
        System.out.println("slistId: " + slistId);
        System.out.println("registrant: " + registrant);
        System.out.println("timeValueStart: " + timeValueStart);
        System.out.println("timeValueEnd: " + timeValueEnd);
        System.out.println(distributedLists);
        return Result.success(distributedLists);

    }

    @Transactional
    @GetMapping("SalaryDistributionQuery/{id}")
    public Result SalaryDistributionQuery(@PathVariable(name = "id") Long distributedid){
        DistributedList distributedList=distributedListService.getById(distributedid);
        return Result.success(distributedList);
    }

    @Transactional
    @GetMapping("SalaryDistributionQueryDetail/{id}")
    public Result SalaryDistributionQueryDetail(@PathVariable(name = "id") Long distributedid){
        QueryWrapper<DistributedListDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("distributed_id",distributedid);
        List<DistributedListDetail> distributedListDetails = distributedListDetailMapper.selectList(queryWrapper);
        return Result.success(distributedListDetails);
    }
}
